$(document)
		.ready(
				function() {
					
					//add alpanumeric validaion constrains
					$.validator.addMethod('alphanumeric', function(value, element) {
						return this.optional(element) || /^\w+$/i.test(value);
					}, 'Letters, numbers, and underscores only please');

									
					var submitRegisterForm = function(){
						$.ajax({
							url : '../auth/register.json',
							type : 'POST',
							data : $('#register').serialize(),

							success : function(data, status) {
								if (data.success) {
									//redirect 
									$("div.error").remove();
									location.href = data.targetUrl;	
								} else {
									//TODO add binding errors messages extraction and assign error message to form fields
									
									//build error list
									var errorList = '<ul>';
									if(data.bindingResult){										
										var errors  = data.bindingResult.bindingErrors;
										console.log(data.bindingResult);
										console.log(errors);
										for(var i = 0; i < errors.length; i++ ){
											var error = errors[i];
											console.log(error);
											
											errorList += '<li>' + error.fieldName + ': ' + error.defaultMessage + '</li>';
										}
									}
									errorList += '</ul>';
									
									$("div.error").remove();
									$('#register')
											.before(
													'<div class="error">Your registration attempt was not successful<br /> Reason: '
															+ data.errorMessage + '</br>'
															+ errorList + '</br>'
															+ 'Please try again.</p></div>');
																		
								}
							},

							error : function(request, status,
									error) {
								$("div.error").remove();
								$('#register')
										.before(
												'<div class="error">Your registration attempt was not successful<br /><br /> Please try again.</p></div>');
							}
						});

					};
					
					
					$("#login").focus();
					
					
					$('#register').validate({
						errorClass: 'error',
						errorElement: 'span',

						submitHandler: submitRegisterForm,
						
						rules:{							
							login: {
								required: true,
								alphanumeric: true
							},
							
							password: {
								required: true,
								minlength: 5
							},
							
							confirmPassword: {
								required: true,
								equalTo: '#password'
							},
							
							email: {
								required:true,
								email: true
							}
							
						},
						messages: {							
							login: {
								required: 'required',
								alphanumeric: 'only alphanumeric chars allowed'
							},		
					
							password: {
								required: 'required',
								minlength: 'minimum 5 chars'
							},
							
							confirmPassword: {
								required: 'required',
								equalTo: 'password not match'
							},
							
							email: {
								required: 'required',
								email: 'wrong email format'
							}			
						}		
					});
					
					//register submit handler
					$('#dfregister-btn')
							.click(
									function(e) {
										e.preventDefault();

										// submition
										$.ajax({
											url : '../auth/register.json',
											type : 'POST',
											data : $('#register').serialize(),

											success : function(data, status) {
												if (data.success) {
													//redirect 
													location.href = data.targetUrl;	
												} else {
													$("div.error").remove();
													$('#register')
															.before(
																	'<div class="error">Your registration attempt was not successful<br /> Reason: '
																			+ data.errorMessage
																			+ '<br /> Please try again.</p></div>');
													
													//TODO add binding errors messages extraction and assign error message to form fields
												}
											},

											error : function(request, status,
													error) {
												$("div.error").remove();
												$('#register')
														.before(
																'<div class="error">Your registration attempt was not successful<br /><br /> Please try again.</p></div>');
											}
										});

									});

				});